<template>
	<view class="content">
		<view class="tips_box">
			<view>* 打印过程中，请不要关闭此界面</view>
		</view>
		<view class="print_msg_box">
			<view class="printing_box">
				<view>正在打印第：</view>
				<view class="printing_msg">
					<text class="printing_count">{{now_count}}</text>
					<text>单</text>
				</view>
			</view>
			<view class="total_box">
				<view>共计：</view>
				<view class="total_msg">
					<text class="printing_count">{{all_count}}</text>
					<text>单</text>
				</view>
			</view>
			<view class="state_box">
				<view>当前状态：</view>
				<view class="state_msg">
					<text :class="state_color">{{state_text}}</text>
				</view>
			</view>
			<view class="btn_box">
				<!-- 打印机出错 或正在打印 显示disabled开始按钮 -->
				<button class="disabled_start_btn" v-show="disabled_start_show">开始打印</button>
				<!-- 初始页面 显示开始打印按钮 -->
				<button class="start_btn" @click="start_print" v-show="start_show">开始打印</button>
				<!-- 打印完成显示重新打印 -->
				<button class="start_again_btn" @click="again_print" v-show="again_show">重新打印</button>
				<!-- 打印未开始 打印结束  -->
				<button class="disabled_pause_btn" v-show="disabled_pause_show">暂停打印</button>
				<!-- 开始打印 打印正常 -->
				<button class="pause_btn" @click="pause_print" v-show="pause_show">暂停打印</button>
				<!-- 暂停后显示继续 打印出错显示继续 -->
				<button class="continue_btn" @click="continue_print" v-show="continue_show">继续打印</button>
				<!-- 打印中 显示结束打印 -->
				<button class="finish_btn" @click="finish_print" v-show="finish_show">结束打印</button>
				<button class="disabled_finish_btn" v-show="!finish_show">结束打印</button>
			</view>
		</view>
	</view>
</template>

<script>
	var get_order_timer = null
	var search_timer = null
	// const tspl = require('../../static/tspl-exp.js');
	// const ToBase64 = require('../../static/base64gb2312.js');
	// const esc = require('../../static/esc.js')
	// const tsc = require('../../static/tsc.js')
	const encode = require('../../static/js/encoding.js')
	export default {
		data() {
			return {
				order_batch_uuid: '',
				order_uuid_list: [],
				token: '',
				// 当前打印状态
				state_text: '待打印',
				device_name: '',
				// 当前张数
				now_count: 0,
				all_count: 0,
				deviceId: '',
				serviceId: '',
				notifyCharacteristicId: '',
				writeCharacteristicId: '',
				// 主线程状态 1：占用 2：空闲
				print_state: 0,
				// 打印机状态 20：打印中 00 空闲 41开盖 04 缺纸 1 占用
				state: '',
				// 当前正在打印的面单uuid
				current_print_uuid: '',
				start_show: true,
				again_show: false,
				disabled_pause_show: true,
				pause_show: false,
				continue_show: false,
				finish_show: false,
				disabled_start_show: false,
				state_color: 'green',
				index: 0,
				// 重新打印面单uuid
				again_uuid: null,
				// 重新打印状态
				again_state: 0,
			}
		},
		onLoad (options) {
			// 禁止息屏
			uni.setKeepScreenOn({
			    keepScreenOn: true,
				success: res => {
					console.log(res)
				}
			});
			console.log(options)
			if (options.batch_uuid){
				this.order_batch_uuid = options.batch_uuid
			}
			if (options.uuid) {
				this.again_uuid = options.uuid
			}
			uni.getStorage({
				key: 'token',
				success: res => {
					this.token = res.data
					// 获取订单uuid列表
					this.getUuidList()
					//初始化蓝牙模块
					// this.openBluetoothAdapter()
				}
			})
			
		},
		// 页面关闭后
		onHide () {
			// todo 结束打印 结束监听 结束推入数据 销毁监听计时器和打印计时器
			clearInterval(get_order_timer)
			clearInterval(search_timer)
		},
		//监听页面卸载
		onUnload (){
			clearInterval(get_order_timer)
			clearInterval(search_timer)
			// let pages = getCurrentPages().length - 1;
			// 	console.log('需要销毁的页面：'+pages);
			// 	wx.navigateBack({
			// 	delta: pages
		 //    })
		},
		methods: {
			// 开始打印 点击事件
			start_print () {
				// 初始化蓝牙
				this.openBluetoothAdapter()
				this.start_show = false
				this.disabled_start_show = true
				this.pause_show = true
				this.disabled_pause_show = false
				this.continue_show = false
				this.finish_show = true
				// 重新打印状态设置为0
				this.again_state = 0
			},
			// 初始化蓝牙
			openBluetoothAdapter() {
				uni.openBluetoothAdapter({
					success: res => {
						console.log(res)
						// 取上一次连接值连接 如果没取到跳转搜索页 状态改为正在连接打印机
						uni.getStorage({
							key: 'last_device_id',
							success: res => {
								console.log(res)
								// 上一次连接设备的设备id
								this.deviceId = res.data
								uni.getStorage({
									key: 'last_device_name',
									success: res3 => {
										// 连接蓝牙 开始打印
										this.device_name = res3.data
										// 连接设备
										this.createBLEConnection();
									}
								})
							},
							fail: res3 => {
								console.log(res3)
								// 如果没有上一次的连接值 跳转到搜索蓝牙设备界面
								uni.redirectTo({
									url: '../addDevice/addDevice'
								})
							}
						})
					},
					// 初始化蓝牙失败
					fail: res => {
						console.log(res)
						// 没开启蓝牙提示 改变状态文字按钮状态
						this.create_fail_change()
						uni.showModal({
							title: '提示',
							content: '初始化蓝牙失败 ，请打开蓝牙与位置信息功能，检查是否允许微信访问位置信息后重试',
							showCancel: false
						});
					}
				})
			},
			// 连接蓝牙
			createBLEConnection() {
				console.log(this.deviceId)
				uni.createBLEConnection({
				    // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
				    deviceId: this.deviceId,
				    success: res => {
						console.log(res)
						if (res.errCode == 0) {
							// 蓝牙连接成功 设置打印机状态的初始值 空闲
							this.state = '00'
							this.state_text = '正在连接打印机...'
							this.state_color = 'blue'
							// 获取服务id 特征id
							this.getBLEDeviceServices()
						}
					},
					// 连接打印机失败
					fail: res => {
						console.log(this.deviceId)
						console.log(res)
						if (res.errCode == -1){
							// 蓝牙连接成功 设置打印机状态的初始值 空闲
							this.state = '00'
							this.state_text = '正在连接打印机...'
							this.state_color = 'blue'
							// 获取服务id 特征id
							this.getBLEDeviceServices()
						} else {
							// 改变按钮与状态文字
							this.create_fail_change()
							// 提示打印机是否未开启
							uni.showModal({
								title: '提示',
								content: '连接蓝牙设备失败 ，请检查蓝牙设备是否开启后重试',
								showCancel: false,
								success: res => {
									if (res.confirm) {
										if (this.order_batch_uuid !== '') {
											// 跳批次面单列表
											uni.redirectTo({
												url: `../batchDetail/batchDetail?uuid=${this.order_batch_uuid}`
											})
										} else if (this.order_batch_uuid == ''){
											uni.redirectTo({
												url: '../orderList/orderList'
											})
										} else {
											uni.redirectTo({
												url: '../index/index'
											})
										}
									}
								}
							});
						}
					}
				})
			},
			// 获取服务id
			getBLEDeviceServices () {
				console.log('进入服务')
				uni.getBLEDeviceServices({
				  // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
					deviceId: this.deviceId,
					success: res => {
						console.log('device services:', res.services)
						// 选择下标为1的serviceID
						this.serviceId = res.services[1].uuid
						// 获取特征id
						this.getBLEDeviceCharacteristics(this.deviceId,res.services[1].uuid)
					},
					fail: res => {
						// 获取服务ID失败 改变按钮状态
						this.create_fail_change()
						uni.showModal({
							title:'提示',
							content: '获取设备服务失败，请检查蓝牙设备状态后重试',
							showCancel: false
						})
					}
			 	})
			},
			// 获取特征id
			getBLEDeviceCharacteristics(deviceId, serviceId){
				console.log("进入特征");
			 	uni.getBLEDeviceCharacteristics({
			 		// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
			 		deviceId,
			 		// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
			 		serviceId,
			 		success:(res) => {
			 			// 获取notifyid 写死
			 			this.notifyCharacteristicId = res.characteristics[0].uuid;
			 			// 获取写id
			 			for (var i = 0; i < res.characteristics.length; i++) {
			 				console.log('特征值：' + res.characteristics[i].uuid)
			 				if (res.characteristics[i].properties.write) {
			 					console.log("writeCharacteristicsId：", res.characteristics[i].uuid);
			 					this.writeCharacteristicId = res.characteristics[i].uuid;
			 				}
			 			}
			 			// 获取特征值成功 存储设备id服务id特征id 
						let device_data = {
							deviceId: this.deviceId,
							serviceId: this.serviceId,
							deviceName: this.device_name,
							notifyCharacteristicId: this.notifyCharacteristicId,
							writeCharacteristicId: this.writeCharacteristicId
						}
						uni.setStorage({
							key: 'device_data',
							data: device_data
						})
						// 上报连接成功的设备
			 			this.$https.post(`api/mini_customer/v1/bluetooth_devices/add`,{
			 				token: this.token,
			 				name: this.device_name,
			 				device_id: deviceId
			 			})
			 			.then( res => {
			 				console.log('----上报成功,开启监听蓝牙状态----')
							// 监听蓝牙连接状态 断开 设备丢失 连接异常
							this.onBLEConnectionStateChange();
							// 开启notify 监听打印机状态
							this.notifyBLECharacteristicValueChange(this.notifyCharacteristicId,deviceId,serviceId)
							this.onBLECharacteristicValueChange()
							// 循环获取数据后打印
							this.getWriteContent()
							// 循环查询打印机状态 10毫秒一次
							this.write_state_timer()
			 			})
						.catch(error=>{
							console.log(error)
						})
						
			 		},
			 		fail: res => {
						// 未获取到特征id 属于连接失败 改变按钮显示与状态文字
						this.create_fail_change()
			 			uni.showModal({
			 				title:'提示',
			 				content: '获取设备特征失败，请检查蓝牙设备状态后重试',
			 				showCancel: false
			 			})
			 		}
			 	})
			},
			//获取打印数据
			getWriteContent () {
				console.log('---进入获取打印数据---')
				let _this = this;
				// 循环获取 获取后打印 打印后重新循环获取 取到最后一个则停止
				let count = this.order_uuid_list.length;
				get_order_timer = setInterval(() => {
					console.log('进入循环：' + get_order_timer)
					console.log(this.index)
					if (this.index >= count){
						// 全部推入结束 改变按钮显示与状态文字 结束计时器
						this.print_finish_change()
						console.log('get_order_timer:' + get_order_timer)
						clearInterval(get_order_timer)
						clearInterval(search_timer)
					}
					// 主线程为占用状态 退出循环
					
					console.log(this.index)
					if (_this.print_state != 0){
						return
					}
					
					console.log(this.index)
					// 打印机状态不是空闲 退出循环
					if (_this.state != '00'){
						return
					}
					
					console.log(this.index)
					// 两个状态都改变 防止取上一张打印后的 state状态
					_this.print_state = 1
					_this.state = '20'
					// 推入数据前 给当前正在打印的面单赋值 用于打印完成上报
					_this.current_print_uuid = _this.order_uuid_list[_this.index]
					// 判断是否是重新打印
					let reprint;
					if (this.again_state === 0) {
						reprint = false
					} else {
						reprint = true
					}
					_this.$https.get(`api/mini_customer/v1/orders/express_bill_info?uuid=${_this.order_uuid_list[_this.index]}&token=${_this.token}&reprint=${reprint}`)
					.then(res => {
						_this.state_text = '正在打印'
						_this.state_color = 'blue'
						// 页面显示当前正在打印第多少张
						_this.now_count = _this.index
						// 打印
						_this.senBlData(_this.deviceId, _this.serviceId, _this.writeCharacteristicId,_this.getPrintContent(res.data))
					})
					this.index = this.index + 1;
					console.log('循环第' + this.index +  '次结束：' + get_order_timer)
				},300)
			},
			// 暂停打印
			pause_print () {
				console.log('get_order_timer:' + get_order_timer)
				clearInterval(get_order_timer)
				this.pause_change()
			},
			// 继续打印
			continue_print () {
				this.getWriteContent()
				this.pause_show = true
				this.disabled_pause_show = false
				this.continue_show = false
			},
			// 结束打印
			finish_print () {
				let _this = this
				// 暂停打印 后提示
				clearInterval(get_order_timer)
				uni.showModal({
					title: '提示',
					content: '是否确认结束打印',
					showCancel: true,
					success: res => {
						if (res.confirm) {
							clearInterval(search_timer)
							_this.index = 0;
							if (this.order_batch_uuid !== '') {
								// 跳批次面单列表
								uni.redirectTo({
									url: `../batchDetail/batchDetail?uuid=${this.order_batch_uuid}`
								})
							} else if (this.order_batch_uuid == ''){
								uni.redirectTo({
									url: '../orderList/orderList'
								})
							} else {
								uni.redirectTo({
									url: '../index/index'
								})
							}
						} else {
							this.getWriteContent() 
						}
					}
				})
			},
			// 重新打印
			again_print () {
				this.index = 0;
				this.again_show = false
				// 重新打印状态为1
				this.again_state = 1
				this.openBluetoothAdapter()
				this.start_show = false
				this.disabled_start_show = true
				this.pause_show = true
				this.disabled_pause_show = false
				this.continue_show = false
				this.finish_show = true
			},
			// 循环查询打印机状态 200毫秒一次
			write_state_timer () {
				search_timer = setInterval(() => {
					if (this.print_state !== 1) {
						// 向打印机发送查询命令
						var content2 = "!?";
						var code = new encode.TextEncoder(
						  'gb18030', {
						    NONSTANDARD_allowLegacyEncoding: true  
						  }).encode(content2)
						  // console.log(code)
						  var command = [];
						  for (var i = 0; i < code.length; ++i) {
						  		  // 转后的数组压入
						    command.push(code[i])
						  }
						  // console.log(command)
						this.senBlSearchData(this.deviceId, this.serviceId, this.writeCharacteristicId,command)
					} else {
						return false
					}
				},200)
			},
			// 监听onBLEConnectionStateChange回调事件，监听蓝牙链接状态 断开后返回数据
			onBLEConnectionStateChange () {
				uni.onBLEConnectionStateChange( res => {
					console.log(res)
					if (res.connected === false) {
						// 断开后暂停打印 重新连接 连接成功后 如果还未打印 那么重新开始打印 如果已经开始 那么显示继续打印按钮 点击后继续打印 如果连接失败 刷新uuid列表 用户手动重新开始
						clearInterval(get_order_timer)
						clearInterval(search_timer)
						this.state_text = '蓝牙断开连接'
						this.state_color = 'red'
						this.print_state = 0
						this.state = ''
						uni.showModal({
							title: '提示',
							content: '蓝牙连接已断开，请重试',
							showCancel:false,
							success: res => {
								if (res.confirm){
									if (this.order_batch_uuid != '') {
										uni.redirectTo({
											url: `../batchDetail/batchDetail?uuid=${this.order_batch_uuid}`
										})
									}else {
										uni.redirectTo({
											url: '../defaultOrderList/defaultOrderList'
										})
									}
								}
							}
						})
						uni.removeStorage({
							key: 'device_data'
						})
						console.log('---重新连接---')
						// this.createBLEConnection()
					}
				})
			},
			// 获取订单uuid列表
			getUuidList () {
				if (this.again_uuid) {
					this.all_count = 1;
					this.order_uuid_list = [this.again_uuid]
				} else {
					this.$https.get(`api/mini_customer/v1/orders/list_uuid?token=${this.token}&order_batch_uuid=${this.order_batch_uuid}`)
					.then(res => {
						console.log(res)
						this.order_uuid_list = res.data.uuids
						this.all_count = res.data.uuids.length
					})
				}
			},
			//启用notify功能
			notifyBLECharacteristicValueChange (characteristicId,deviceId,serviceId) {
				uni.notifyBLECharacteristicValueChange({
					state: true, // 启用 notify 功能
					// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
					deviceId,
					// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
					serviceId,
					// 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
					characteristicId,
					success(res) {
						console.log(res)
						console.log('notifyBLECharacteristicValueChange success', res.errMsg)
					}
				})
			},
			// 向打印机写入数据
			senBlData(deviceId, serviceId, characteristicId,uint8Array) {
				let _this = this
				var uint8Buf = Array.from(uint8Array);
				// 分包
				function split_array(datas,size){
					var result = [];
					var j = 0
					for (var i = 0; i < datas.length; i += size) {
						result[j] = datas.slice(i, i + size)
						j++
					}
					console.log(result)
					return result
				}
				var sendloop = split_array(uint8Buf, 20);
				console.log(sendloop.length)
				function realWriteData(sendloop, i) {
					var data = sendloop[i]
					// 推入结束判断
					if(typeof(data) == "undefined"){
						// 写入后打印机未立即打印 阻塞到打印开始 放开 不影响获取的打印机状态
						function sleep(d){
						  for(var t = Date.now();Date.now() - t <= d;);
						}
						sleep(500)
						//推完上报打印完成
						// _this.$https.patch(`api/mini_customer/v1/orders/update_state`,{
						// 	token: _this.token,
						// 	uuid: _this.current_print_uuid,
						// 	state: 'wait_collect'
						// })
						// .then(res => {
						// 	console.log('打印完成上报成功')
						// })
						// 判断是否是重新打印
						let reprint;
						if (_this.again_state === 0) {
							reprint = false
						} else if (_this.again_state === 1) {
							reprint = true
						}
						_this.$https.post(`api/mini_customer/v1/orders/update_state`,{
							token: _this.token,
							uuid: _this.current_print_uuid,
							state: 'wait_collect',
							reprint: reprint,
							_method: "patch"
						})
						.then( res => {
							console.log('打印完成上报成功')
						})
						// 改变主线程状态为空闲
						_this.print_state = 0
						return
					}
					// console.log("第【" + i + "】次写数据"+data)
					var buffer = new ArrayBuffer(data.length)
					var dataView = new DataView(buffer)
					for (var j = 0; j < data.length; j++) {
						dataView.setUint8(j, data[j]);
					}
					uni.writeBLECharacteristicValue({
						deviceId,
						serviceId,
						characteristicId,
						value: buffer,
						success(res) {
							// console.log("write success:");
							// console.log(res);
							realWriteData(sendloop, i + 1);
						},
						fail (res) {
							console.log(res)
							// todo 写入失败 提示写入失败 检查状态 重新连接
							uni.showModal({
								title:'提示',
								content:'打印机通讯失败，请检查蓝牙设备状态后重试',
								showCancel:false,
								success: resq => {
									if (resq.confirm) {
										if (res.errCode == 10006) {
											this.state_text = '蓝牙断开连接'
											this.state_color = 'red'
											// 连接断开 主线程未占用 没有打印机状态 删除已连接的设备信息 自动重新连接
											this.print_state = 0
											this.state = ''
											uni.removeStorage({
												key: 'device_data'
											})
											console.log('---重新连接---')
											if (this.order_batch_uuid != '') {
												uni.redirectTo({
													url: `../batchDetail/batchDetail?uuid=${this.order_batch_uuid}`
												})
											}else {
												uni.redirectTo({
													url: '../defaultOrderList/defaultOrderList'
												})
											} 
										}
									}
								}
							})
						}
					})
				}
			   var i = 0;
				realWriteData(sendloop, i);
			},
			// 查询写入
			senBlSearchData(deviceId, serviceId, characteristicId,uint8Array) {
				let _this = this
				// console.log('************deviceId = [' + deviceId + ']  serviceId = [' + serviceId + '] characteristics=[' +characteristicId+ "]")
				let uint8Buf = Array.from(uint8Array);
				// 分包
				function split_array(datas,size){
					let result = {};
					let j = 0
					for (let i = 0; i < datas.length; i += size) {
						result[j] = datas.slice(i, i + size)
						j++
					}
					return result
				}
				let sendloop = split_array(uint8Buf, 20);
				// console.log(sendloop.length)
				function realWriteData(sendloop, i) {
					let data = sendloop[i]
					if(typeof(data) == "undefined"){
						return
					}
					// console.log("第【" + i + "】次写数据"+data)
					let buffer = new ArrayBuffer(data.length)
					let dataView = new DataView(buffer)
					for (let j = 0; j < data.length; j++) {
						dataView.setUint8(j, data[j]);
					}
					// 主线程占用
					if (_this.print_state !== 1){
					uni.writeBLECharacteristicValue({
						deviceId,
						serviceId,
						characteristicId,
						value: buffer,
						success(res) {
							// console.log("write success:");
							// console.log(res);
							realWriteData(sendloop, i + 1);
						},
						fail (res) {
							console.log(res)
							// 写入失败重新连接
							_this.createBLEConnection()
						}
					})
					}
				}
			   let i = 0;
				realWriteData(sendloop, i);
			},
			ab2hex(buffer) {
			  var hexArr = Array.prototype.map.call(
			    new Uint8Array(buffer),
			    function (bit) {
			      return ('00' + bit.toString(16)).slice(-2)
			    }
			  )
			  return hexArr.join('');
			},
			// 打印完成后变量值改变
			print_finish_change () {
				console.log('---打印完成---')
				this.state_text = '打印完成'
				this.state_color = 'green'
				this.disabled_start_show = false
				this.again_show = true
				this.start_show = false
				this.disabled_pause_show = true
				this.pause_show = false
				this.continue_show = false
				this.finish_show = false
			},
			// 连接过程中失败改变变量值
			create_fail_change () {
				this.state_text = '连接失败'
				this.state_color = 'red'
				this.start_show = true
				this.pause_show = false
				this.disabled_pause_show = true
				this.continue_show = false
				this.finish_show = false
				this.disabled_start_show = false
			},
			// 监听打印机状态返回值方法
			onBLECharacteristicValueChange () {
				uni.onBLECharacteristicValueChange((res) => {
					// console.log("res:");
					// console.log(res);
					if ((this.ab2hex(res.value) != '00') && (this.ab2hex(res.value) != '20')) {
						this.print_state = 0
						this.pause_print()
						this.pause_change()
					}
					console.log(this.ab2hex(res.value));
					// 如果主线程在占用状态 那么状态就是正在打印 不修改为打印机空闲 
					if (this.ab2hex(res.value) == '04'){
						this.state_text = '打印机缺纸'
						this.state_color = 'yellow'
						this.state = this.ab2hex(res.value)
						// todo 调用暂停打印方法与打印机状态异常时按钮显示改变方法
						this.pause_print()
						this.pause_change()
					} else if (this.ab2hex(res.value) == '41'){
						this.state_text = '打印机开盖'
						this.state_color = 'yellow'
						this.state = this.ab2hex(res.value)
						// todo 调用暂停打印方法与打印机状态异常时按钮显示改变方法
						this.pause_print()
						this.pause_change()
					}
					if (this.print_state != 1){
						this.state = this.ab2hex(res.value)
					}
				});
			},
			// 状态异常时暂停按钮显示
			pause_change () {
				this.pause_show = false
				this.continue_show = true
				this.disabled_pause_show = false
			},
			// 获取打印面单模板
			getPrintContent(print_data) {
				let content = "SIZE 98 mm,177 mm\r\n"
				content += "GAP 3 mm\r\n"
				content += "CLS\r\n"
				content += "DIRECTION 1\r\n"
				content += "TEXT 176,24,\"1\",0,2,2,\"" + print_data.agent_fund.slice(0,2) +"\"\r\n"
				content += "TEXT 176,56,\"1\",0,2,2,\"" + print_data.agent_fund.slice(2,4) + "\"\r\n"
				content += "BOLD 1\r\n"
				content += "TEXT 24,160,\"9\",0,3,3,\"" + print_data.shorting_code + "\"\r\n"
				content += "BOLD 0\r\n"
				content += "TEXT 24,272,\"9\",0,1,1,\"寄件人：\"\r\n"
				content += "TEXT 120,272,\"9\",0,1,1,\"" + print_data.sender_name + "\"\r\n"
				content += "TEXT 256,272,\"9\",0,1,1,\"" + print_data.sender_phone + "\"\r\n"
				let sender_address_top_height = 303;
				if (print_data.sender_address.length != 0){
					for (let i = 0;i < print_data.sender_address.length; i++) {
						content += "TEXT 120," + sender_address_top_height + ",\"9\",0,1,1,\"" + print_data.sender_address[i] + "\"\r\n"
						sender_address_top_height = sender_address_top_height + 30
					}
				}
				content += "TEXT 24,389,\"9\",0,1,1,\"收件人：\"\r\n"
				content += "BOLD 1\r\n"
				content += "TEXT 120,389,\"1\",0,2,2,\"" + print_data.receiver_name + "\"\r\n"
				content += "BOLD 1\r\n"
				content += "BOLD 1\r\n"
				content += "TEXT 120,427,\"1\",0,2,2,\"" + print_data.receiver_phone + "\"\r\n"
				content += "BOLD 0\r\n"
				let receiver_address_top_height = 467;
				if (print_data.receiver_address.length != 0){
					for (let i = 0;i < print_data.sender_address.length; i++) {
						content += "TEXT 120," + receiver_address_top_height + ",\"9\",0,1,1,\"" + print_data.receiver_address[i] + "\"\r\n"
						receiver_address_top_height = receiver_address_top_height + 30
					}
				}
				content += "TEXT 24,556,\"9\",0,1,1,\"备注：\"\r\n"
				let receiver_remark_height = 556
				if (print_data.receiver_remark.length != 0) {
					for (let i = 0; i < print_data.receiver_remark.length; i++ ){
						content += "TEXT 120," + receiver_remark_height + ",\"1\",0,2,2,\"" + print_data.receiver_remark[i] + "\"\r\n"
						receiver_remark_height = receiver_remark_height + 30
					}
				}
				content += "TEXT 24,775,\"9\",0,1,1,\"打印时间：\"\r\n"
				content += "TEXT 136,775,\"9\",0,1,1,\"" + print_data.print_at + "\"\r\n"
				if (print_data.current_print_count && print_data.total_print_count){
					content += "TEXT 136,807,\"9\",0,1,1,\"" + print_data.current_print_count + " / " + print_data.total_print_count + "\"\r\n"
				}
				content += "TEXT 392,775,\"9\",0,1,1,\"代收货款：\"\r\n"
				content += "TEXT 504,775,\"9\",0,2,2,\"￥" + print_data.agent_fee + "元\"\r\n"
				content += "BAR 24,128,736,2\r\n"
				content += "BAR 24,256,736,2\r\n"
				content += "BAR 24,373,736,2\r\n"
				content += "BAR 24,540,736,2\r\n"
				content += "BAR 24,746,736,2\r\n"
				content += "BAR 384,758,2,74\r\n"
				content += "BAR 24,988,736,2\r\n"
				content += "BAR 24,1080,736,2\r\n"
				content += "BAR 24,1226,736,2\r\n"
				content += "BARCODE 264,16,\"128\",72,1,0,4,6,\"" + print_data.express_code + "\"\r\n"
				content += "BARCODE 24,880,\"128\",72,1,0,4,6,\"" + print_data.express_code + "\"\r\n"
				content += "TEXT 526,884,\"1\",0,2,2,\"" + print_data.agent_fund.slice(0,2) +"\"\r\n"
				content += "TEXT 526,916,\"1\",0,2,2,\"" + print_data.agent_fund.slice(2,4) +"\"\r\n"
				content += "TEXT 24,992,\"9\",0,1,1,\"寄件人：\"\r\n"
				content += "TEXT 120,992,\"9\",0,1,1,\"" + print_data.sender_name + "\"\r\n"
				content += "TEXT 256,992,\"9\",0,1,1,\"" + print_data.sender_phone + "\"\r\n"
				let sender_address_bottom_height = 1023;
				if (print_data.sender_address.length != 0){
					for (let i = 0;i < print_data.sender_address.length; i++){
						content += "TEXT 120," + sender_address_bottom_height + ",\"9\",0,1,1,\"" + print_data.sender_address[i] + "\"\r\n"
						sender_address_bottom_height = sender_address_bottom_height + 30
					}
				}
				content += "TEXT 24,1096,\"9\",0,1,1,\"收件人：\"\r\n"
				content += "TEXT 120,1096,\"9\",0,1,1,\"" + print_data.receiver_name + "\"\r\n"
				content += "TEXT 120,1127,\"9\",0,1,1,\"" + print_data.receiver_phone + "\"\r\n"
				let receiver_address_bottom_height = 1158;
				if (print_data.receiver_address.length != 0){
					for (let i = 0; i < print_data.receiver_address.length; i++) {
						content += "TEXT 120," + receiver_address_bottom_height + ",\"9\",0,1,1,\"" + print_data.receiver_address[i] + "\"\r\n"
						receiver_address_bottom_height = receiver_address_bottom_height + 30
					}
				}
				content += "TEXT 24,1236,\"1\",0,2,2,\"收件人签字：\"\r\n"
				content += "TEXT 392,1236,\"9\",0,1,1,\"代收货款：\"\r\n"
				content += "TEXT 504,1236,\"9\",0,2,2,\"￥" + print_data.agent_fee + "元\"\r\n"
				content += "PRINT 1,1\r\n"
				let code = new encode.TextEncoder(
					'gb18030', {
						NONSTANDARD_allowLegacyEncoding: true  
					}).encode(content)
				console.log(code)
				let command = [];
				for (let q = 0; q < code.length; ++q) {
				  	// 转后的数组压入 
				    command.push(code[q])
				}
				return command
			},
		}
	}
</script>

<style>
	.content {
		height: 100%;
		width: 100%;
		overflow-x: hidden;
	}
	.tips_box{
		width: 100%;
		height: 100rpx;
		background-color: #D1E0DA;
	}
	.tips_box view{
		line-height: 100rpx;
		font-size: 24rpx;
		color: #13A684;
		padding-left: 24rpx;
	}
	.print_msg_box,.total_msg_box{
		width: 702rpx;
		background: #ffffff;
		height: 504rpx;
		border-radius: 12rpx;
		margin-top: 24rpx;
		margin-left: 24rpx;
	}
	.printing_box{
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-left: 24rpx;
		padding-top: 52rpx;
		padding-bottom: 24rpx;
		width: calc(100% - 48rpx);
		border-bottom: 2rpx solid  #EEEEEE;
		font-size: 32rpx;
		color: #333333;
	}
	.total_box{
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-left: 24rpx;
		padding-top: 24rpx;
		padding-bottom: 24rpx;
		width: calc(100% - 48rpx);
		border-bottom: 2rpx solid  #EEEEEE;
		font-size: 32rpx;
		color: #333333;
	}
	.printing_msg{
		display: flex;
	}
	.printing_count{
		color: #13A684;
		margin-right: 24rpx;
		font-weight: bold;
	}
	.state_box{
		display: flex;
		margin-top: 24rpx;
		font-size: 32rpx;
		color: #333333;
		align-items: center;
		justify-content: space-between;
		width: calc(100% - 48rpx);
		margin-left: 24rpx;
	}
	.state_msg{
		font-weight: bold;
	}
	.btn_box{
		width: calc(100% - 48rpx);
		margin-left: 24rpx;
		margin-top: 120rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
	.btn_box button{
		width: 202rpx;
		height: 80rpx;
		border-radius: 12rpx;
		text-align: center;
		align-items: 80rpx;
		font-size: 32rpx;
		border: none!important
	}
	button::after { border: none }
	.disabled_start_btn,.disabled_pause_btn,.disabled_finish_btn{
		color: white;
		background: #D8D8D8;
		border: none!important;
	}
	.start_btn,.start_again_btn{
		background-color: #13A684;
		color: #ffffff;
	}
	.pause_btn,.continue_btn{
		background: #FFB63A;
		color: #ffffff;
	}
	.finish_btn{
		background-color: #ff5f5f;
		color: #ffffff;
	}
	.green{
		color: #13A684
	}
	.blue{
		color: #3A9BFF;
	}
	.yellow{
		color: #FFB63A;
	}
	.red{
		color: #ff5f5f
	}
</style>
